package com.oa.item.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.oa.item.biz.ItemBiz;
import com.oa.item.entity.Item;
import com.oa.item.entity.ItemWithBLOBs;

@Controller
@RequestMapping("/item")
public class ItemController {
	@Resource
	private ItemBiz itemBiz;
	
	@RequestMapping("/findAll")
	public String findAll(Model model,@RequestParam(name="page", defaultValue="1") Integer page){
		List<Item> itemList=itemBiz.selectAllItem();
		List<Item> itemList1= new ArrayList<>();
		int total=0;
		if(itemList.size()>=page*5){
			for (int i = (page-1)*5; i < page*5; i++) {
				itemList1.add(itemList.get(i));
			}
		}else{
			for (int i = (page-1)*5; i < itemList.size(); i++) {
				itemList1.add(itemList.get(i));
			}
		}
		if(itemList.size()%5==0){
			total=itemList.size()/5;
		}else{
			total=itemList.size()/5+1;
		}
		model.addAttribute("itemList1", itemList1);
		model.addAttribute("page", page);
		model.addAttribute("total", total);
		return "item/list";
	}
	
	@RequestMapping("find/{id}")
	public String find(Model model,@PathVariable("id")Integer id){
		ItemWithBLOBs iwb=itemBiz.selectItemById(id);
		model.addAttribute("iwb", iwb);
		return "item/item";
	}
}
